package data

import (
	"encoding/json"
)

// a rune slice
type SliceSliceRune struct {
	SlicerBase
}

// a cmp method for []runes
func cmpSliceRune(i1 interface{}, i2 interface{}) int {
	var i, length int
	var r1, r2 rune
	var rr1, rr2 []rune
	rr1 = i1.([]rune)
	rr2 = i2.([]rune)
	length = len(rr1)
	i = len(rr2)
	if length > i {
		length = i
	}
	for i = 0; i < length; i++ {
		r1 = rr1[i]
		r2 = rr2[i]
		if r1 > r2 {
			return 1
		} else if r1 < r2 {
			return -1
		}
	}
	// they are equal
	return 0
}

// Builds a rune slice.
func BuildSliceSliceRune(length, capacity int) SliceSliceRune {
	var v SliceSliceRune
	v.Init(length, capacity, cmpSliceRune)
	return v
}

// Builds a pointer to a rune slice.
func NewSliceSliceRune(length, capacity int) *SliceSliceRune {
	var v SliceSliceRune
	v = BuildSliceSliceRune(length, capacity)
	return &v
}

// Returns the item at index.
func (this *SliceSliceRune) AtIndex(index int) []rune {
	var ifc interface{}
	ifc = this.SlicerBase.AtIndex(index)
	if ifc != nil {
		return ifc.([]rune)
	}
	return nil
}

// Remove gets a []rune at index
// Deletes the []rune at index form the array
// Returns the []rune.
func (this *SliceSliceRune) Remove(index int) []rune {
	var ifc interface{}
	ifc = this.SlicerBase.Remove(index)
	if ifc != nil {
		return ifc.([]rune)
	}
	return nil
}

// Returns the data as a slice.
func (this *SliceSliceRune) Slice() [][]rune {
	var i, length int
	var item interface{}
	var ss [][]rune
	length = len(this.Data)
	ss = make([][]rune, length, length)
	for i, item = range this.Data {
		ss[i] = item.([]rune)
	}
	return ss
}

// Marshals the data as an array.
func (this *SliceSliceRune) MarshalJSON() ([]byte, error) {
	return json.Marshal(this.Slice())
}
